﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
<<<<<<<< HEAD:MedicalPCTerminal.Write.API/Appcation/CommandHandlers/BasicSettingsCommandHandler/AddIndexDefineCommandHandler.cs
using MedicalPCTerminal.Write.API.Appcation.Commands.BasicSettingsCommand;
using Yitter.IdGenerator;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.BasicSettingsCommandHandler
========
using MedicalPCTerminal.Write.API.Appcation.Commands.基础设置;
using Yitter.IdGenerator;

namespace MedicalPCTerminal.Write.API.Appcation.CommandHandlers.基础设置
>>>>>>>> parent of fe317f5 (修改命名空间):MedicalPCTerminal.Write.API/Appcation/CommandHandlers/基础设置/AddIndexDefineCommandHandler.cs
{
    public class AddIndexDefineCommandHandler : IRequestHandler<AddIndexDefineCommand, APIResult<object>>
    {
        private readonly SqlSugarContext sqlSugar;

        private readonly IMapper mapper;

        public AddIndexDefineCommandHandler(SqlSugarContext sqlSugar, IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }

        public Task<APIResult<object>> Handle(AddIndexDefineCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<IndexDefine>(request);
            if(model.IndexDefine_Id == 0)
            {
                //添加
                model.IndexDefine_Code = YitIdHelper.NextId();
                var r = sqlSugar.db.Insertable(model).ExecuteCommand();
                if (r > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                    result.data = model;
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                    result.data = null;
                }
            }
            else
            {
                //修改
                var r = sqlSugar.db.Updateable(model).ExecuteCommand();
                if (r > 0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = model;
                }
                else
                {
                    result.Code = APIEnum.修改失败;
                    result.Msg = APIEnum.修改失败.ToString();
                    result.data = null;
                }
            }
            return Task.FromResult(result);
        }
    }
}
